package com.origon360.Services;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.origon.entities.Deal;
import com.origon.entities.Product;
import com.origon.entities.Shop;

@Stateless
public  class ProductService {
	
	@PersistenceContext
	EntityManager em;
	
	
	public void createProduct(Product product ){
		em.persist(product);
	}
	
	
	public List<Product> fetchProducts(){
		return em.createQuery("FROM Prodcut", Product.class).getResultList();
	}
	
	public List<Deal> fetchDeals(){
		return em.createQuery("FROM Deal", Deal.class).getResultList();	
	}
	
	public List<Shop> fetchShopsPerUser(String userId){
		List<Shop> userShops = em.createQuery("FROM Shop s JOIN FETCH s.user WHERE s.user.id =:userId ", Shop.class)
				.setParameter("userId", userId).getResultList();
		return userShops;
	}

	public List<Product> fetchProductsPerShop(String shopId){
			List<Product> productsPerShop = em.createQuery("FROM Product p JOIN FETCH p.shop WHERE p.shop.id =:shopId", Product.class)
					.setParameter("shopId", shopId).getResultList();
		return productsPerShop;
	}
}
